package com.ctvit.sjk.knowledge.core.controller;

import com.alibaba.fastjson2.JSONObject;
import com.ctvit.sjk.knowledge.app.pojo.vo.AppAuthenticationVO;
import com.ctvit.sjk.knowledge.core.page.TableRecordInfo;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * web层知识库数据处理
 *
 * @author heyingcheng
 * @date 2025/03/23
 */
public class KnowledgeController extends BaseController {

    /**
     * 创建ID
     */
    protected static final String CREATE_ID = "1";

    @Value("${token.secret}")
    protected String secret;

    protected String createToken(Map<String, Object> claims) {
        return Jwts.builder()
                .setClaims(claims)
                .signWith(SignatureAlgorithm.HS512, secret)
                .compact();
    }

    protected Claims parseToken(String token) {
        return Jwts.parser()
                .setSigningKey(secret)
                .parseClaimsJws(token)
                .getBody();
    }

    protected AppAuthenticationVO parseAppAuthentication(HttpServletRequest request) {
        String authorization = request.getHeader("Authorization");
        Claims claims = parseToken(authorization);
        AppAuthenticationVO appAuthenticationVO = new AppAuthenticationVO();
        appAuthenticationVO.setApplicationId(ObjectUtils.defaultIfNull((String) claims.get(AppAuthenticationVO.KEY_APPLICATION_ID), StringUtils.EMPTY));
        appAuthenticationVO.setUserId(ObjectUtils.defaultIfNull((String) claims.get(AppAuthenticationVO.KEY_USER_ID), StringUtils.EMPTY));
        appAuthenticationVO.setAccessToken(ObjectUtils.defaultIfNull((String) claims.get(AppAuthenticationVO.KEY_ACCESS_TOKEN), StringUtils.EMPTY));
        appAuthenticationVO.setClientId(ObjectUtils.defaultIfNull((String) claims.get(AppAuthenticationVO.KEY_CLIENT_ID), StringUtils.EMPTY));
        appAuthenticationVO.setType(ObjectUtils.defaultIfNull((String) claims.get(AppAuthenticationVO.KEY_TYPE), StringUtils.EMPTY));
        appAuthenticationVO.setAuthentication(ObjectUtils.defaultIfNull(claims.get(AppAuthenticationVO.KEY_AUTHENTICATION), new JSONObject()));
        return appAuthenticationVO;
    }


    /**
     * 设置请求分页数据
     */
    protected void startPage(int pageNum, int pageSize) {
        PageMethod.startPage(pageNum, pageSize, "").setReasonable(true);
    }

    /**
     * 响应返回结果
     *
     * @param data 结果
     * @return 操作结果
     */
    protected AjaxResult toAjax(String data) {
        AjaxResult result = success();
        result.put(AjaxResult.DATA_TAG, data);
        return result;
    }

    protected TableRecordInfo getRecordTable(List<?> list, int pageNum, int pageSize) {
        TableRecordInfo rspRecord = new TableRecordInfo();
        rspRecord.setCode(HttpStatus.SUCCESS);
        rspRecord.setMessage("查询成功");
        rspRecord.setRecords(list);
        rspRecord.setTotal(new PageInfo<>(list).getTotal());
        rspRecord.setCurrent(pageNum);
        rspRecord.setSize(pageSize);
        return rspRecord;
    }

}
